
define(['jquery',
    'backbone',
    'models/message',
    'collections/businesses',
    'collections/user',
    'collections/messageThreads',
    'collections/messages',
    'collections/appointment',
    'views/left_nav',
    'views/business/list',
    'views/business/details',
    'views/service/select-list',
	'views/business/booking-appointment',
    'views/service/list',
    'views/service/details',
    'views/service/details-appointment-form',
    'views/message/thread-list',
    'views/message/thread-details',
    'views/message/list',
    'views/message/details',
    'views/message/new-message',
    'views/appointment/agenda',
    'views/appointment/calendar',
    'views/user/employeeList',
    'bgstretcher'
], function($, Backbone,
            Message,
            BusinessCollection,
            UserCollection,
            ThreadMessageCollection,
            MessageCollection,
            AppointmentCollection,
            LeftNavView,
            BusinessListView,BusinessDetailView,
            ActiveListDropDown,
			BookingAppointmentForm,
            ServiceListView,ServiceDetailView,ServiceDetailAppFormView,
            ThreadMessageListView,ThreadMessageDetailView,
            MessageListView,MessageDetailView,NewMessageForm,
            AgendaView,
            AppointmentCalView,
            EmployeeListView){
    Backbone.View.prototype.close = function () {
        if (this.beforeClose) {
            this.beforeClose();
        }
        this.remove();
        this.unbind();
    };


    return  Backbone.Router.extend({
		
		routes:{
            "":"list",
            "business/list":"list",
            "business/new":"newBusiness",
            "business/:id":"businessDetails",
            "business/:id/booking-appointment":"bookingAppointment",
			"appointment/index":"appointmentSchedule",
            "thread/message":"threadMessage",
            "thread/:id":"threadMessageDetail",
            "message/inbox":"inboxMessage",
            "message/new":"messageNew",
            "message/:id":"messageDetails",
            "employee/list":"employeeManager",
            "business/update/:id":"updateBusinessDetails",
            "business/:business_id/service/:id":"serviceDetail",
            "business/:business_id/service/:id/make-appointment":"newAppointmentViaService",
            "user/list"            : "userList",
            "user/new"             : "userNew",
            "user/edit/:id"        : "userEdit",
            "user/delete/:id"      : "userDelete"
		},

        list:function () {
            this.businessBefore();
        },
        showLeftNav:function(){
            var leftNavView=new LeftNavView();
            $('#nav-sidebar').html(leftNavView.render().el);
        },
        businessDetails:function (id) {

            this.businessBefore(function () {
                var business = app.businessList.get(id);
                //console.log(business);
                var businessDetailView=new BusinessDetailView({model:business});
                app.showView('#content', businessDetailView);
                var services=business.get('services');//get nested model
                /*console.log(services);*/
                //app.showView('#service-list', businessDetailView);
                 var serviceListView=new ServiceListView({model:services});
                $('#service-list').html(serviceListView.render().el);
//                if (serviceListView.scrollRender) {
//                    serviceListView.scrollRender();
//                }
                //Refresh profile background
                $('#business-header').bgStretcher({
                    images: [''],
                    imageWidth: 1024,
                    imageHeight: 768,
                    slideDirection: 'N',
                    slideShowSpeed: 1000,
                    transitionEffect: 'fade',
                    sequenceMode: 'normal',
                    anchoring: 'left center',
                    anchoringImg: 'left center'
                });
            });
        },
		
		bookingAppointment: function(id){
			this.businessBefore(function () {
                var business = app.businessList.get(id);
                var bookingAppointmentForm=new BookingAppointmentForm({model:business});
                app.showView('#content', bookingAppointmentForm);
                var services=business.get('services');//get nested model
                _.each(services.models, function (service) {
                    var temp=service.toJSON();
                    var option='<option value="'+temp.id+'">'+temp.name+'</option>';
                    $("#serviceList").append(option);
                });
            });
		},

        serviceDetail:function (business_id,id) {

            this.businessBefore(function () {
                var business = app.businessList.get(business_id);

                var services=business.get('services').get(id);//get nested model
                var serviceDetailView=new ServiceDetailView({model:services});
                app.showView('#content', serviceDetailView);
                /*console.log(services);*/
                //app.showView('#service-list', businessDetailView);
               /* var serviceListView=new ServiceListView({model:services});
                $('#service-list').html(serviceListView.render().el);*/
                /*if (serviceDetailView.scrollRender) {
                    serviceDetailView.scrollRender();
                }*/
                //Refresh profile background
                $('#business-header').bgStretcher({
                    images: [''],
                    imageWidth: 1024,
                    imageHeight: 768,
                    slideDirection: 'N',
                    slideShowSpeed: 1000,
                    transitionEffect: 'fade',
                    sequenceMode: 'normal',
                    anchoring: 'left center',
                    anchoringImg: 'left center'
                });
            });
        },
        newAppointmentViaService:function (business_id,id) {

            this.businessBefore(function () {
                var business = app.businessList.get(business_id);

                var services=business.get('services').get(id);//get nested model
                var serviceDetailAppFormView=new ServiceDetailAppFormView({model:services});
                app.showView('#content', serviceDetailAppFormView);
            });
        },

        showView:function (selector, view) {
            if (this.currentView)
                this.currentView.close();
            $(selector).html(view.render().el);
            this.currentView = view;
            return view;
        },
        //For business listing.
        businessBefore:function (callback) {

            if (this.businessList) {
                var businessListView=new BusinessListView({model:app.businessList});
                    $('#sidebar div.bs-docs-sidebar').html(businessListView.render().el);

                    if (callback) callback();
            } else {
                this.businessList = new BusinessCollection();
                var businessListView=new BusinessListView({model:app.businessList});
                this.businessList.fetch({success:function () {
                    $('#sidebar div.bs-docs-sidebar').html(businessListView.render().el);
                    if (callback) callback();
                }});
            }
        },
        //For employee listing.
        employeeManager:function () {
            this.employeeBefore();
        },

        employeeBefore:function (callback) {

            if (this.employeeList) {
                /*this.employeeList = new UserCollection();
                this.employeeList.url='api/employees';*/
                var employeeListView=new EmployeeListView({model:app.employeeList});
                /*this.employeeList.fetch({success:function () {*/
                    $('#sidebar div.bs-docs-sidebar').html(employeeListView.render().el);

                    if (callback) callback();
                    /*if (employeeListView.scrollRender) {
                        employeeListView.scrollRender();
                    }*/
                /*}});*/
                if (callback) callback();
            } else {
                this.employeeList = new UserCollection();
                this.employeeList.url='api/employees';
                var employeeListView=new EmployeeListView({model:app.employeeList});
                this.employeeList.fetch({success:function () {
                    $('#sidebar div.bs-docs-sidebar').html(employeeListView.render().el);

                    if (callback) callback();
                    /*if (employeeListView.scrollRender) {
                        employeeListView.scrollRender();
                    }*/
                }});
            }
        },

        threadBefore:function (callback) {

            if (this.threadMessageList) {
                /*this.messageList=new MessageCollection();*/
                var threadMessageListView=new  ThreadMessageListView({model:app.threadMessageList});
//                this.businessList = new BusinessCollection();
//                var businessListView=new BusinessListView({model:app.businessList});
                /*this.messageList.fetch({success:function () {*/
                    $('#sidebar div.bs-docs-sidebar').html(threadMessageListView.render().el);

                    if (callback) callback();
                    /*if (messageListView.scrollRender) {
                        messageListView.scrollRender();
                    }*/
                /*}});*/
                if (callback) callback();
            } else {
                this.threadMessageList=new ThreadMessageCollection();
                var threadMessageListView=new  ThreadMessageListView({model:app.threadMessageList});
//                this.businessList = new BusinessCollection();
//                var businessListView=new BusinessListView({model:app.businessList});
                this.threadMessageList.fetch({success:function () {
                    $('#sidebar div.bs-docs-sidebar').html(threadMessageListView.render().el);

                    if (callback) callback();
                    /*if (messageListView.scrollRender) {
                        messageListView.scrollRender();
                    }*/
                }});
            }
        },
        threadMessage:function () {
            this.threadBefore();
        },



        threadMessageDetail:function(id){
            this.threadBefore(function () {
                var threadMessage = app.threadMessageList.get(id);
                var threadMessageDetailView=new ThreadMessageDetailView({model:threadMessage});
                app.showView('#content', threadMessageDetailView);
                var messages=threadMessage.get('messages');//get nested model
//                //app.showView('#service-list', businessDetailView);
                var messageListView=new MessageListView({model:messages});
                $('#message-list').html(messageListView.render().el);
                /*if (messageDetailView.scrollRender) {
                 messageDetailView.scrollRender();
                 }*/
                //Refresh profile background

            });
        },

        messageNew:function(){
            this.threadBefore(function () {
                app.showView('#content', new NewMessageForm({model:new Message()}));
            });
        },

        messageDetails:function (id) {

            this.threadBefore(function () {
                var message = app.messageList.get(id);
                var messageDetailView=new MessageDetailView({model:message});
                app.showView('#content', messageDetailView);
//                var services=business.get('services');//get nested model
//                //app.showView('#service-list', businessDetailView);
//                var serviceListView=new ServiceListView({model:services});
//                $('#service-list').html(serviceListView.render().el);
                /*if (messageDetailView.scrollRender) {
                    messageDetailView.scrollRender();
                }*/
                //Refresh profile background

            });
        },
		appointmentSchedule:function(){
			this.appointmentBefore();
		},

        //For appointment listing.
        appointmentBefore:function (callback) {
            if (this.appointmentList) {
//                Declare the appointment (event) list in calendar view
                var agendaView=new AgendaView({model:app.appointmentList});

                var appointmentCalView=new AppointmentCalView({collection:app.appointmentList});
                //Render the agenda on the left .
                $('#sidebar div.bs-docs-sidebar').html(agendaView.render().el);
                //Render the full calendar with appointment filled in.
                $('#content').html(appointmentCalView.render().el);

                if (callback) callback();
            } else {
                //appointment list initializing.
                this.appointmentList = new AppointmentCollection();
                //For the simple listing on the left.
                var agendaView=new AgendaView({model:app.appointmentList});
                //For the calendar.
                var appointmentCalView=new AppointmentCalView({collection:app.appointmentList});
                this.appointmentList.fetch({success:function () {
                    //Render the agenda on the left .
                    $('#sidebar div.bs-docs-sidebar').html(agendaView.render().el);
                    //Render the full calendar with appointment filled in.
                    $('#content').html(appointmentCalView.render().el);
                    if (callback) callback();
                }});
            }
            setTimeout('$(window).resize();',500);
        }
	});

});

